using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace OllieExplorer
{
    //NOTE: We only really need with, but to speed things up a bit I've added start and end
    public class GridColumn
    {
        private int _End;
        private int _Start;
        private int _Width;
        private string _ColumnHeading = String.Empty;
        private readonly IList<string> _Data = new List<string>();
        private bool _Dirty = true;
        private Font _Font = SystemFonts.DefaultFont;
        //private int _RowHeight;
        //private int _ColumnHeadingHeight;
        internal delegate void GridColumnEvent(GridColumn gridColumn);

        internal GridColumnEvent DataDirty;
        internal GridColumnEvent Resized;
        // TODO: Split this up so that individual cells and column headings get the event
        public MouseEventHandler MouseMoved;
        public MouseEventHandler MouseMovedUp;
        public MouseEventHandler MouseMovedDown;

        private int _Id;

        public int Id
        {
            get
            {
                return _Id;
            }
            internal set
            {
                _Id = value;
            }

        }

        internal GridColumn()
        {
            //_RowHeight = _Font.Height;
            //_ColumnHeadingHeight = _Font.Height + 2;
        }
        public Font Font
        {
            get
            {
                return _Font;
            }
            set
            {
                _Font = value;
                MakeDirty();
            }
        }

        public IList<string> Data
        {
            get
            {
                return _Data;
            }
        }

        public string ColumnHeadding
        {
            get
            {
                return _ColumnHeading;
            }
            set
            {
                _ColumnHeading = value;
                MakeDirty();
            }
        }

        public bool Dirty
        {
            get
            {
                return _Dirty;
            }
            internal set
            {
                _Dirty = value;
            }
        }

        public void MakeDirty()
        {
            _Dirty = true;
            if (DataDirty != null)
            {
                DataDirty(this);
            }
        }

        internal int End
        {
            get
            {
                return _End;
            }
            set
            {
                _End = value;
                _Width = (_End - _Start) + 1;
            }
        }

        internal int Start
        {
            get
            {
                return _Start;
            }
            set
            {
                _Start = value;
            }
        }

        public int Width
        {
            get
            {
                return _Width;
            }
            set
            {
                _Width = value;
                if (Resized != null)
                {
                    Resized(this);
                }
            }
        }

        internal void Draw(Graphics graphics, int x, int columnHeadingHeight, int rowHeight, int startRow, bool showColumnHeading)
        {
            int yoffset = 0;
            graphics.FillRectangle(SystemBrushes.MenuBar, new Rectangle(x, yoffset, _Width, columnHeadingHeight));
            graphics.DrawString(_ColumnHeading, _Font, SystemBrushes.MenuText, x, yoffset + (columnHeadingHeight - _Font.Height) / 2);
            yoffset += columnHeadingHeight;
            int counter = startRow;
            while (counter < _Data.Count && yoffset < graphics.ClipBounds.Height)
            {

                graphics.DrawString(_Data[counter], _Font, SystemBrushes.WindowText, x, yoffset + (rowHeight - _Font.Height) / 2);
                yoffset += rowHeight;
                counter++;
            }

        }
    }
}
